//
//  YKBadgeView.m
//  youkan
//
//  Created by mac mini on 16/2/25.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKBadgeView.h"
#define YKBadegeViewFont [UIFont systemFontOfSize:11]
@implementation YKBadgeView

-(instancetype)initWithFrame:(CGRect)frame{

    if (self=[super initWithFrame:frame]) {
        self.userInteractionEnabled =NO;
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        //设置字体的大小
        self.titleLabel.font =YKBadegeViewFont;
        // 宽度由图片决定
        [self sizeToFit];
    }
    return self;
}

-(void)setBadgeValue:(NSString *)badgeValue{
    _badgeValue =badgeValue;
//    判断badgeValue是否有内容
    if (badgeValue.length==0 ||[badgeValue isEqualToString:@"0"]) {
        self.hidden = YES;
    }else{
        self.hidden = NO;
    }
//    CGSize size = [badgeValue sizeWithFont:YKBadegeViewFont]; 计算内容的大小
    CGSize size = [badgeValue sizeWithAttributes:@{NSFontAttributeName:YKBadegeViewFont}];
//    YKLog(@"%f---%f",size.width,self.width);
    //!> 设置内容时只设置按钮图片或者是有文字图片和背景图片
    if (size.width>self.width) {
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        [self setTitle:nil forState:UIControlStateNormal];
        [self setBackgroundImage:nil forState:UIControlStateNormal];
    }else{
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        [self setTitle:badgeValue forState:UIControlStateNormal];
        [self setImage:nil forState:UIControlStateNormal];
    }
}
@end
